//
//  QJShouKuanVC.m
//  QuanJieShopProject
//
//  Created by apple on 2018/3/2.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJShouKuanVC.h"
#import "QJMoneyPayVC.h"
#import "QJLoginVC.h"
#import "YFUserStatusAndTypeTool.h"

#import "QJMineBankVC.h"
#import "QJMineNewShouYiVC.h"

@interface QJShouKuanVC ()

@property (nonatomic, strong) UIView *codePayView; //  二维码付款页面
@property (nonatomic, strong) UIView *loginView;   //  登录按钮界面
@property (nonatomic, strong) UIView *normalView;  //  只有商家收款提醒

@property (nonatomic, strong) UIImageView *saomaImgView; // 二维码图片

@property (nonatomic, strong) UIBarButtonItem *rightButton;
@end

@implementation QJShouKuanVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"我要收款";
    self.extendedLayoutIncludesOpaqueBars = YES;

    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.codePayView.hidden = YES;
    self.loginView.hidden = YES;
    self.normalView.hidden = YES;
    
//    [self hiddenRightBarItem];
    

    if ([QJUserInfo isLogin]) {

        [YFUserStatusAndTypeTool fetchUserStatus:^(NSInteger status, NSInteger userType) {
            
            if (status == 1) {  // 审核通过
                // 是商家的时候更新本地数据
                UserInfoModel *model = [QJUserInfo getCurrentUser];
                model.user_type = @"3";
                [QJUserInfo saveCurrentUser:model];
                
                self.codePayView.hidden = NO;
                [self loadCodeImg];
            }else {
                self.normalView.hidden = NO;
            }
        }];
       
    }else{  // 从登录页面返回,又到登录界面去了
        self.loginView.hidden = NO;
    }

}


- (void)hiddenRightBarItem {
    UIButton *rightButton = [[UIButton alloc] init];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}



- (UIView *)loginView {
    if (!_loginView) {
        self.navigationItem.rightBarButtonItem = nil;

        _loginView = [[UIView alloc] initWithFrame:CGRectMake(0, Height_NavBar, UISCREEN_WIDTH, UISCREEN_HEIGHT - Height_NavBar - Height_TabBar)];
        [self.view addSubview:_loginView];
        
        // 登录按钮
        UIButton *loginBtn = [[UIButton alloc] init];
        loginBtn.frame = CGRectMake(0, 0, 100, 40);
        loginBtn.center = CGPointMake(_loginView.mj_w/2, _loginView.mj_h/2);
        [loginBtn setTitle:@"去登陆" forState:UIControlStateNormal];
        [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        loginBtn.backgroundColor = YFColor_Red;
        loginBtn.layer.cornerRadius = 5;
        [loginBtn addTarget:self action:@selector(goToJump) forControlEvents:UIControlEventTouchUpInside];
        [_loginView addSubview:loginBtn];
    }
    return _loginView;
}

- (UIView *)normalView {
    if (!_normalView) {
        self.navigationItem.rightBarButtonItem = nil;
        
        _normalView = [[UIView alloc] initWithFrame:CGRectMake(0, Height_NavBar, UISCREEN_WIDTH, UISCREEN_HEIGHT - Height_NavBar - Height_TabBar)];
        [self.view addSubview:_normalView];
        
        /*
         2018-05-14 09:20:18.474 QuanJieShopProject[49835:14070714] view.mj_h = 568.000000
         2018-05-14 09:20:18.474 QuanJieShopProject[49835:14070714] view.center.y = 284.000000
         2018-05-14 09:20:18.474 QuanJieShopProject[49835:14070714] _normalView.mj_h = 455.000000
         2018-05-14 09:20:18.474 QuanJieShopProject[49835:14070714] _normalView.center.y = 291.500000
         */
//        NSLog(@"view.mj_h = %f", self.view.mj_h);
//        NSLog(@"view.center.y = %f", self.view.center.y);
//        NSLog(@"_normalView.mj_h = %f", _normalView.mj_h);
//        NSLog(@"_normalView.center.y = %f", _normalView.center.y);

        // 会员提醒
        UILabel *label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentCenter;
        label.frame = CGRectMake(0, 0, UISCREEN_WIDTH, 25);
        label.center = CGPointMake(_normalView.mj_w/2, _normalView.mj_h/2);
//        _normalView.center;
        
        label.text = @"只有泉界商家才能进行收款操作!";
        label.textColor = YFColor_Red;
        [_normalView addSubview:label];
    }
    return _normalView;
}



- (UIView *)codePayView {
    if (!_codePayView) {
        _codePayView = [[UIView alloc] initWithFrame:CGRectMake(0, Height_NavBar, UISCREEN_WIDTH, UISCREEN_HEIGHT - Height_NavBar)];
        _codePayView.backgroundColor = YFColor_Red;
        [self.view addSubview:_codePayView];
        
        // 现金收款
        
        // 二维码
        CGFloat padding = 10;
        CGFloat contentViewW = UISCREEN_WIDTH - 2*padding;
        CGFloat contentViewH = contentViewW;
        UIView *codeContentView = [[UIView alloc] init];
        codeContentView.backgroundColor = [UIColor whiteColor];
        codeContentView.frame = CGRectMake(padding, padding, contentViewW, contentViewH);
        [_codePayView addSubview:codeContentView];
        
        
        UIImageView *saomaImgView = [[UIImageView alloc] init];
        saomaImgView.frame = CGRectMake(0, 0, contentViewW *0.6, contentViewH * 0.6);
        saomaImgView.center = CGPointMake(contentViewW/2, contentViewH/2);
//        saomaTipView.backgroundColor = [UIColor blueColor];
        [codeContentView addSubview:saomaImgView];
        self.saomaImgView = saomaImgView;
    
        
        // 二维码 提示
        CGFloat labelH = 20;
        CGFloat labelY = CGRectGetMinY(saomaImgView.frame) - labelH;
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(0, labelY, _codePayView.frame.size.width, labelH);
        label.text = @"扫一扫, 向我付款";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:14];
        label.textColor = YFColor_Text;
        [_codePayView addSubview:label];

        //
        UIView *jumpContentView = [[UIView alloc] init];
        jumpContentView.frame = CGRectMake(padding, CGRectGetMaxY(codeContentView.frame) + padding, contentViewW, 200.f/1334.f * UISCREEN_HEIGHT);
        jumpContentView.backgroundColor = [UIColor whiteColor];
        [_codePayView addSubview:jumpContentView];
        
        CGFloat btnWH = 80.f/750.f * UISCREEN_HEIGHT;
        CGFloat btnY = (jumpContentView.mj_h - btnWH)/2;
        CGFloat shouyiBtnX = jumpContentView.mj_w/4 - btnWH/2;
        UIButton *shouyiBtn = [[UIButton alloc] init];
        shouyiBtn.frame = CGRectMake(shouyiBtnX, btnY, btnWH, btnWH);
        [shouyiBtn setTitle:@"我的收益" forState:UIControlStateNormal];
        [shouyiBtn setTitleColor:YFColor_Text forState:UIControlStateNormal];
        shouyiBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [shouyiBtn setImage:[UIImage imageNamed:@"shouyi"] forState:UIControlStateNormal];
        [shouyiBtn layoutButtonWithEdgeInsetsStyle:LLButtonStyleTextBottom imageTitleSpace:10];
        [shouyiBtn addTarget:self action:@selector(shouyiBtnClick) forControlEvents:UIControlEventTouchUpInside];
//        shouyiBtn.backgroundColor = [UIColor blueColor];
        [jumpContentView addSubview:shouyiBtn];
        
        
        CGFloat kabaoBtnX = jumpContentView.mj_w* 3.f/4.f - btnWH/2;
        UIButton *kabaoBtn = [[UIButton alloc] init];
        kabaoBtn.frame = CGRectMake(kabaoBtnX, btnY, btnWH, btnWH);
        [kabaoBtn setTitle:@"我的卡包" forState:UIControlStateNormal];
        [kabaoBtn setTitleColor:YFColor_Text forState:UIControlStateNormal];
        kabaoBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [kabaoBtn setImage:[UIImage imageNamed:@"kabao"] forState:UIControlStateNormal];
        [kabaoBtn layoutButtonWithEdgeInsetsStyle:LLButtonStyleTextBottom imageTitleSpace:10];
        [kabaoBtn addTarget:self action:@selector(kabaoBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [jumpContentView addSubview:kabaoBtn];
        
    }
    
    return _codePayView;
    
}

- (void)loadCodeImg {
    
    UserInfoModel *model = [QJUserInfo getCurrentUser];
    NSString *codeImageStr = model.pay_url;
//    NSLog(@"urlImg = %@", [NSString stringWithFormat:@"%@%@",NET_BASEURL_IMG,codeImageStr]);
    [self.saomaImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@upload/%@",NET_BASEURL_IMG,codeImageStr]] placeholderImage:KImageName(@"noImage")];
}

#pragma mark - private
- (void)shouyiBtnClick {
    QJMineNewShouYiVC *VC = [[QJMineNewShouYiVC alloc]init];
    KPushViewControllerAction(VC);
}

- (void)kabaoBtnClick {
    [MBPHUD showAlertWithTitle:@"暂未开发"];
    return;
    
    QJMineBankVC *VC = [[QJMineBankVC alloc]init];
    VC.typeStr = @"mine";
    KPushViewControllerAction(VC);
}

- (void)rightBackItemClicked{
    QJMoneyPayVC *VC = [[QJMoneyPayVC alloc]init];
    KPushViewControllerAction(VC);
}

#pragma mark - 去登陆
- (void)goToJump {
    QJLoginVC *VC = [[QJLoginVC alloc]init];
    [self.navigationController pushViewController:VC animated:YES];
}


#pragma mark - 懒加载
- (UIBarButtonItem *)rightButton {
    if (!_rightButton) {
        
        UIButton *rightButton = [[UIButton alloc] init];
        [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rightButton.titleLabel setFont:[UIFont systemFontOfSize:17]];
        //    [rightButton sizeToFit];
        rightButton.frame = CGRectMake(0, 0, 120, 30);
        [rightButton addTarget:self action:@selector(rightBackItemClicked) forControlEvents:UIControlEventTouchUpInside];
        [rightButton setTitle:@"现金收款" forState:UIControlStateNormal];
        UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
        self.navigationItem.rightBarButtonItem = rightBarItem;
        
        _rightButton = rightBarItem;
    }
    return _rightButton;
}

@end
